<template>
  <div class="historical-data">
    <el-card :body-style="{ padding: '0px' }">
      <div slot="header">
        <tools-btn
          :toolsData="toolsData"
          @addData="addData"
          @refresh="refresh"
          @search="search"
          @fieldSet="fieldSet"
          @hasOperation="hasOperation"
        />
      </div>
      <!-- card body -->
      <table-list
        id="tableExcel"
        :table="table"
        ref="tableList"
        @handleEdit="handleEdit"
        @handleDel="handleDel"
      >
        <template
          slot-scope="props"
          slot="fan1"
        >
          <span
            :class="props.obj.row.fan1==='0'?'typeSuccsee':'typeError'"
            v-text="props.obj.row.fan1==='0'?'通电':'断电'"
          />
        </template>
        <template
          slot-scope="props"
          slot="fan2"
        >
          <span
            :class="props.obj.row.fan2==='0'?'typeSuccsee':'typeError'"
            v-text="props.obj.row.fan2==='0'?'通电':'断电'"
          />
        </template>
        <template
          slot-scope="props"
          slot="cleaner1"
        >
          <span
            :class="props.obj.row.cleaner1==='0'?'typeSuccsee':'typeError'"
            v-text="props.obj.row.cleaner1==='0'?'通电':'断电'"
          />
        </template>
        <template
          slot-scope="props"
          slot="cleaner2"
        >
          <span
            :class="props.obj.row.cleaner2==='0'?'typeSuccsee':'typeError'"
            v-text="props.obj.row.cleaner2==='0'?'通电':'断电'"
          />
        </template>
        <template
          slot-scope="props"
          slot="deviceStatus"
        >
          <span
            :class="props.obj.row.deviceStatus==='0'?'typeSuccsee':'typeError'"
            v-text="props.obj.row.deviceStatus==='0'?'正常':'异常'"
          />
        </template>
      </table-list>

      <pagination
        :page="page"
        @pageSizeChange="pageSizeChange"
        @pageCurrentChange="pageCurrentChange"
      />
    </el-card>
    <!-- 添加、修改弹出框 -->
    <el-dialog
      :title="editDialog.title"
      v-if="editDialog.visible"
      :visible.sync="editDialog.visible"
      @close="closeDialog('editDialog')"
      width="30%"
    >
      <el-form
        :model="editDialog.data"
        ref="editDialog.data"
        label-width="80px"
      >
        <el-form-item label="名 称">
          <el-input
            v-model="editDialog.data.name"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="代 码">
          <el-input
            v-model="editDialog.data.num"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="图 片">
          <el-input
            v-model="editDialog.data.urlImg"
            size="mini"
          />
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button
          size="small"
          plain
          @click="closeDialog('editDialog')"
        >取 消</el-button>
        <el-popover
          class="edit-dialog-popover"
          placement="top"
          v-model="editDialog.submitVisible"
        >
          <p style="text-align: center; margin-top: 0; color:#409eff">
            确定提交吗？
          </p>
          <div style="text-align: center">
            <el-button
              size="mini"
              @click="editDialog.submitVisible = false"
              v-text="'取 消'"
              plain
            />
            <el-button
              type="primary"
              size="mini"
              v-text="'确 定'"
              @click="handleSubmit"
              plain
            />
          </div>
          <el-button
            slot="reference"
            type="primary"
            size="small"
            v-text="'提 交'"
            plain
          />
        </el-popover>
      </span>
    </el-dialog>
    <!-- 搜索弹出框 -->
    <el-dialog
      :title="searchDialog.title"
      v-if="searchDialog.visible"
      :visible.sync="searchDialog.visible"
      @close="closesearchDialog('searchDialog')"
      width="31%"
    >
      <el-form
        :model="searchDialog.data"
        ref="searchDialog.data"
        label-width="80px"
      >
        <el-form-item label="设备名称">
          <el-input
            v-model="searchDialog.person_name"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="设备号">
          <el-input
            v-model="searchDialog.id_IMEI"
            size="mini"
          />
        </el-form-item>
       <el-form-item label="设备地址">
          <el-input
            v-model="searchDialog.address"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="searchDialog.time"
            type="datetimerange"
            style="width:100%;"
            size="mini"
            start-placeholder="开始时间"
            end-placeholder="结束时间">
          </el-date-picker>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button
          size="small"
          plain
          @click="closesearchDialog('searchDialog')"
        >取 消</el-button>
        <el-button
            slot="reference"
            type="primary"
            size="small"
            v-text="'确 定'"
            @click="searchSubmit"
            plain
          />
        <!-- <el-popover
          class="edit-dialog-popover"
          placement="top"
          v-model="searchDialog.submitVisible"
        >
          <p style="text-align: center; margin-top: 0; color:#409eff">
            确定提交吗？
          </p>
          <div style="text-align: center">
            <el-button
              size="mini"
              @click="searchDialog.submitVisible = false"
              v-text="'取 消'"
              plain
            />
            <el-button
              type="primary"
              size="mini"
              v-text="'确 定'"
              @click="searchSubmit"
              plain
              id="btn"
            />
          </div>
        </el-popover> -->
      </span>
    </el-dialog>
  </div>
</template>

<script>
import TableList from "../../components/Table/TableList";
import Pagination from "../../components/Pagination/pagination";
import ToolsBtn from "../../components/Tools/ToolsBtn";
import TableProp from "../../utils/tableProps.js";
import PageProp from "../../utils/pageProps1.js";
import HHead from "./HHead.js";
export default {
  name: "HistoricalData",
  data() {
    return {
      table: TableProp,
      page: PageProp,
      toolsData: {
        title: "历史数据",
        datatimefier: true,
        datatime: '',
        identifier: true,
        content: "请输入设备号",
        query: '',
        oBtnShow: [
          { btn0: false }, // 新增按钮
          { btn1: true }, // 刷新按钮
          { btn2: true }, // 搜索按钮
          { btn3: false }, // 设置按钮
          { btn4: true } // 导出Excel按钮
        ]
      },
      QueryParams:{},
      editDialog: {
        title: "编 辑",
        visible: false,
        submitVisible: false,
        data: null
      },
      searchDialog: {
        title: "搜 索",
        visible: false,
        submitVisible: false,
        data: null,
        person_name: '',
        id_IMEI: '',
        address: '',
        time: ''
      }
    };
  },
  components: {
    TableList,
    Pagination,
    ToolsBtn
  },
  created() {
    this.table.tHead = HHead;
    this.table.identifier = true;
    this.table.hasOperation = false;
    this.getList(1, 10);
  },
  methods: {
    // 新增数据
    addData() {
      this.handleEdit();
    },
    // 列表数据刷新
    refresh() {
      this.searchDialog= {
        title: "搜 索",
        visible: false,
        submitVisible: false,
        data: null,
        person_name: '',
        id_IMEI: '',
        address: '',
        time: ''
      }
      this.QueryParams = {}
      this.getList(1, 10);
    },
    // 搜索
    search() {
      this.searchDialog.visible = true;
      this.searchDialog.submitVisible = true;
    },
    searchSubmit(){
      function _p(s) {
        return s < 10 ? "0" + s : s;
      }
      var startTime = ''
      if(this.searchDialog.time[0] === ''){
        startTime = this.searchDialog.time[0]
      } else {
        const inform = new Date(this.searchDialog.time[0]);
        startTime = `${inform.getFullYear()}-${_p(inform.getMonth() + 1)}-${_p(inform.getDate())} ${_p(inform.getHours())}:${_p(inform.getMinutes())}:${_p(inform.getSeconds())}`;
      }
      var endTime = ''
      if(this.searchDialog.time[1] === ''){
        endTime = this.searchDialog.time[1]
      } else {
        const inform = new Date(this.searchDialog.time[1]);
        endTime = `${inform.getFullYear()}-${_p(inform.getMonth() + 1)}-${_p(inform.getDate())} ${_p(inform.getHours())}:${_p(inform.getMinutes())}:${_p(inform.getSeconds())}`;
      }      
      this.searchDialog.id_IMEI === null || this.searchDialog.id_IMEI === ''?null:this.QueryParams.ID_IMEI = this.searchDialog.id_IMEI
      this.searchDialog.person_name === null || this.searchDialog.person_name === ''?null:this.QueryParams.person_name = this.searchDialog.person_name
      this.searchDialog.address === null || this.searchDialog.address === ''?null:this.QueryParams.address = this.searchDialog.address
      this.searchDialog.startTime === null || this.searchDialog.startTime === ''?null:this.QueryParams.startTime = this.searchDialog.startTime
      this.searchDialog.endTime === null || this.searchDialog.endTime === ''?null:this.QueryParams.endTime = this.searchDialog.endTime
      this.getList(1,10)
      this.searchDialog.visible = false;
      this.searchDialog.submitVisible = false;
    },
    // 字段设置
    fieldSet() {
    },
    // 隐藏表格操作列
    hasOperation() {
      // this.table.hasOperation = !this.table.hasOperation;
      this.table.hasOperation = false
    },
    // 获取数据列表
    getList(pageNo, pageSize) {
      this.table.data = [];
      this.table.loading = true;
      this.QueryParams.pageNo = pageNo
      this.QueryParams.pageSize = pageSize
      this.$api.DATA.historyData(this.QueryParams)
        .then(res => {
          const data = res.data;
          const total = res.count;
          this.table.data = data;
          this.page.total = total;
          this.table.loading = false;
        })
        .catch(() => {
          this.$message({
            message:"暂无数据",
            type: 'error'
          });
        });
    },
    // 分页大小
    pageSizeChange(size) {
      const tableDataLength = this.table.data.length;
      this.page.pageSize = size;
      if (tableDataLength < this.page.pageSize) {
        this.getList(1, size);
      } else {
        const pageNo = this.page.currentPage;
        this.getList(pageNo, size);
      }
    },
    // 翻页
    pageCurrentChange(pageNo) {
      this.page.currentPage = pageNo;
      const size = this.page.pageSize;
      this.getList(pageNo, size);
    },
    // 编辑数据模态
    handleEdit(index, row) {
      this.editDialog.visible = true;
      if (index !== undefined && row !== undefined) {
        const title = `${row.name}`;
        const data = row;
        this.editDialog.title = title;
        this.editDialog.data = data;
      } else {
        this.editDialog.title = "添加";
        this.editDialog.data = {};
      }
    },
    // 提交数据
    handleSubmit() {
      this.editDialog.visible = false;
      this.editDialog.submitVisible = false;
    },
    // 删除
    handleDel() {
    },
    // 关闭模态
    closeDialog(name) {
      if (name === "editDialog") {
        this[name].submitVisible = false;
        this[name].data = null;
      }
      this[name].visible = false;
    },
    closesearchDialog(name) {
      if (name === "searchDialog") {
        this[name].submitVisible = false;
        this[name].data = null;
      }
      this[name].visible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.edit-dialog-popover {
  margin-left: 10px;
}
.typeSuccsee {
  color: #67c23a;
}
.typeError {
  color: #f56c6c;
}
/deep/ .el-range-editor--mini.el-input__inner{
  margin-left: 20px;
}
/deep/ .el-dialog .el-dialog__title{
  font-size: 12px;
  
}
/deep/ .el-dialog .el-form-item__label{
    font-size: 12px;
  }
/deep/ .el-range-editor--mini.el-input__inner{
  margin-left: 0;
  width: 100%;
}
</style>